﻿using System;
using System.Collections.Generic;
using System.Linq;
using Xc.DataAccess.Core.Extensions;

namespace Xc.DataAccess.Protocoals.WaterMeters.Models.Requests
{
    /// <summary>
    /// 用户充值指令
    /// </summary>
    public class SetUserChangeRequest : MeterFrameTxBase
    {
        internal protected override DataSegment DataSegment => DataSegment.SetUserChange;

       internal protected override FrameCommand Cmd => FrameCommand.CMD04;
        /// <summary>
        /// 单位代码
        /// </summary>
        public byte UnitId { get; set; }
        /// <summary>
        /// 充值次数
        /// </summary>
        public ushort ChargeCount { get; set; }
        /// <summary>
        /// 充值行为
        /// </summary>
        public ChargeBehavior ChargeBehavior { get; set; }
        protected override void GetDataArea(List<byte> vs)
        {
            vs.Add((byte)ChargeBehavior);
            vs.AddRange(ChargeCount.FromUInt16LittleEndian());
            vs.Add(UnitId);
            List<byte> data = new List<byte>();
            var result = ChargeValue.FromDeciamlUInt32LittleEndian(10.0M);
            data.AddRange(result.Take(3).ToArray());
            data.Add(0);
            byte[] array = new byte[] { 0x69, 0x12, 0x5A, 0x43 };
            for (int i = 0; i < array.Length; i++)
            {
                data[i] ^= array[i];
            }
            vs.AddRange(data);
            var hex = DateTime.Now.ToString("yyMMddHHmmss").ToHex();
            vs.AddRange(hex);
            var random=new Random();
            vs.Add((byte)random.Next(0,255));
            vs.AddRange(Enumerable.Repeat<byte>(0,4));
        }
        /// <summary>
        /// 充值量 m³ 0.1M
        /// </summary>
        public decimal ChargeValue { get; set; }

    }
    /// <summary>
    /// 充值行为
    /// </summary>
    public enum ChargeBehavior:byte
    {
        /// <summary>
        /// 新卡
        /// </summary>
        NewCard=0x01,
        /// <summary>
        /// 旧卡
        /// </summary>
        OldCard=0x11,
        /// <summary>
        /// 补卡
        /// </summary>
        CopyCard=0x55
    }
}
